import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import org.rsbot.event.events.MessageEvent;
import org.rsbot.event.listeners.MessageListener;
import org.rsbot.event.listeners.PaintListener;
import org.rsbot.script.Script;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.methods.Camera;
import org.rsbot.script.methods.Game;
import org.rsbot.script.methods.Inventory;
import org.rsbot.script.methods.Keyboard;
import org.rsbot.script.methods.Mouse;
import org.rsbot.script.methods.Objects;
import org.rsbot.script.methods.Skills;
import org.rsbot.script.util.Timer;
import org.rsbot.script.wrappers.RSItem;
import org.rsbot.script.wrappers.RSObject;
import org.rsbot.script.wrappers.RSPlayer;

@ScriptManifest(authors={"LastCoder"}, keywords={"Auto"}, name="AutoIvy", version=2.0D, description="Start next to ivy!")
public class AutoIvy extends Script
  implements MessageListener, PaintListener
{
  private static final int[] IVY_ID = { 46318, 46320, 46322, 46324 };
  private static final int[] IVY_NEST_ID = { 5070, 5071, 5072, 5073, 5074, 5075, 5076, 7413, 11966 };
  private static final Color COLOR_1 = new Color(0, 0, 0, 155);
  private static final Color COLOR_2 = new Color(0, 0, 0);
  private static final Color COLOR_3 = new Color(255, 255, 255);
  private static final BasicStroke STROKE = new BasicStroke(1.0F);
  private static final Font FONT_1 = new Font("Arial", 0, 17);
  private static final Font FONT_2 = new Font("Arial", 0, 9);
  private long activityTime;
  private long startExp;
  private long expGained;
  private long startTime;
  private int expHour;

  private AutoIvy.state getState()
  {
    if (this.inventory.isFull()) {
      return AutoIvy.state.DROP;
    }
    if (!busy()) {
      return AutoIvy.state.CHOP;
    }
    return AutoIvy.state.SLEEP;
  }

  private boolean busy()
  {
    return System.currentTimeMillis() - this.activityTime < 8000L;
  }

  private void antiBan() {
    int i = random(1, 5);
    switch (i) {
    case 1:
      if (random(1, 25) != 1)
        return;
      this.mouse.move(random(10, 750), random(10, 495));
      return;
    case 2:
      if (random(1, 6) != 1)
        return;
      int j = this.camera.getAngle() + random(-45, 45);
      if (j < 0) {
        j = random(0, 10);
      }
      if (j > 359) {
        j = random(0, 10);
      }
      char c = '%';
      if (random(0, 100) < 50)
        c = '\'';
      this.keyboard.pressKey(c);
      sleep(random(100, 500));
      this.keyboard.releaseKey(c);
      return;
    case 3:
      if (random(1, 15) != 1)
        return;
      this.mouse.moveSlightly();
      return;
    }
  }

  public int loop()
  {
    switch (AutoIvy.1.$SwitchMap$AutoIvy$state[getState().ordinal()]) {
    case 1:
      RSItem[] arrayOfRSItem = this.inventory.getItems();
      for (int i = 0; i < arrayOfRSItem.length; i++) {
        if (arrayOfRSItem[i] == null)
          continue;
        if (arrayOfRSItem[i].getID() == IVY_NEST_ID[i])
          arrayOfRSItem[i].doAction("Drop");
      }
      break;
    case 2:
      RSObject localRSObject = this.objects.getNearest(IVY_ID);
      if (localRSObject == null) break;
      if (!localRSObject.isOnScreen()) {
        this.camera.turnToObject(localRSObject);
        for (int j = 0; (j < 100) && (!localRSObject.isOnScreen()); j++)
          sleep(20);
      } else if (getMyPlayer().getAnimation() != -1) {
        this.activityTime = System.currentTimeMillis();
      } else {
        localRSObject.doAction("Chop"); } break;
    case 3:
      sleep(20);
      antiBan();
    }

    return random(600, 1200);
  }

  public boolean onStart()
  {
    log("Start next to ivy!");
    this.startExp = this.skills.getCurrentExp(8);
    this.startTime = System.currentTimeMillis();
    return this.game.isLoggedIn();
  }

  public void messageReceived(MessageEvent paramMessageEvent)
  {
    String str = paramMessageEvent.getMessage();
    if (str.contains("you"))
      this.activityTime = System.currentTimeMillis();
  }

  public void onRepaint(Graphics paramGraphics)
  {
    Graphics2D localGraphics2D = (Graphics2D)paramGraphics;
    long l = System.currentTimeMillis() - this.startTime;
    String str = Timer.format(l);
    if ((this.skills.getCurrentExp(8) - this.startExp > 0L) && (this.startExp > 0L))
    {
      this.expGained = (this.skills.getCurrentExp(8) - this.startExp);
    }
    if ((this.expGained > 0L) && (l > 0L)) {
      this.expHour = (int)(3600L * this.expGained / l);
    }
    localGraphics2D.setColor(COLOR_1);
    localGraphics2D.fillRect(14, 350, 474, 99);
    localGraphics2D.setColor(COLOR_2);
    localGraphics2D.setStroke(STROKE);
    localGraphics2D.drawRect(14, 350, 474, 99);
    localGraphics2D.setFont(FONT_1);
    localGraphics2D.setColor(COLOR_3);
    localGraphics2D.drawString("AutoIvy", 209, 374);
    localGraphics2D.setFont(FONT_2);
    localGraphics2D.drawString("EXP/Hr: " + this.expHour, 18, 390);
    localGraphics2D.drawString("EXP Gained: " + this.expGained, 18, 400);
    localGraphics2D.drawString("Time Ran: " + str, 182, 390);

    localGraphics2D.drawString("Status: " + getState().toString(), 182, 400);
  }
}